package com.kpmg.datalake.web;

import com.alibaba.fastjson.JSONObject;
import com.kpmg.datalake.common.handler.QueryRequestQueue;
import com.kpmg.datalake.operatelog.enums.LogTypeEnum;
import com.kpmg.datalake.operatelog.model.OperateLog;
import com.kpmg.datalake.operatelog.service.IOperateLogService;
import com.kpmg.datalake.reportanalysis.listener.ApplicationStartup;
import java.util.Date;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * 主程序
 * @author Tony
 */
@SpringBootApplication
//@EnableScheduling
@EnableAspectJAutoProxy(exposeProxy = true)
@ComponentScan(basePackages = "com.kpmg.datalake")
public class RunApplication extends WebMvcConfigurerAdapter {

	private static final Logger logger = LoggerFactory.getLogger(RunApplication.class);
	
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
		registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
	}

	/**
	 * 跨域支持
	 * @param registry 跨源请求
	 */
	@Override
	public void addCorsMappings(CorsRegistry registry) {
		registry.addMapping("/**").allowCredentials(true).allowedHeaders("*").allowedOrigins("*").allowedMethods("*");
	}

	public static void main(String[] args) {
		// 开启请求队列监听
    long operationTime = System.currentTimeMillis();
    QueryRequestQueue.getInstance().start();
		SpringApplication springApplication = new SpringApplication(RunApplication.class);
		springApplication.addListeners(new ApplicationStartup());
		long loadParamTime = System.currentTimeMillis();
		ApplicationContext context = springApplication.run(args);
    recordLog(context, loadParamTime,  "加载初始参数");
		String[] activeProfiles = context.getEnvironment().getActiveProfiles();
		for (String activeProfile : activeProfiles) {
			logger.info("Active Profile:{}", activeProfile);
		}

    recordLog(context, operationTime,  "系统启动");
	}

  /**
   * 添加系统启动日志
   * @param context ApplicationContext
   * @param operationTime 开始时间
   * @param logDesc 日志描述
   */
	public static void recordLog(ApplicationContext context, long operationTime, String logDesc){
    OperateLog operateLog = new OperateLog();
    IOperateLogService operateLogService = context.getBean(IOperateLogService.class);
    operateLog.setLogDesc(logDesc);
    operateLog.setLogType(LogTypeEnum.SYSTEM.getCode());
    operateLog.setLogModule("system");
    operateLog.setClassName(RunApplication.class.getName());
    operateLog.setMethodName("main");
    operateLog.setBeginTime(new Date(operationTime));
		operateLog.setRequestParam(JSONObject.toJSONString(context.getEnvironment()));
    long endTime = System.currentTimeMillis();
    operateLog.setEndTime(new Date(endTime));
    operateLog.setCostTime(endTime - operateLog.getBeginTime().getTime());
    operateLog.setRequestStatus(0);

    operateLogService.insert(operateLog);
  }
}
